package com.ruoyi.purchase.controller;

import com.ruoyi.purchase.domain.FirmCommodity;
import com.ruoyi.purchase.service.IFirmCommodityService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/commodity")
public class FirmCommodityController extends BaseController
{
    @Autowired
    private IFirmCommodityService firmCommodityService;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("purchase:commodity:list")
    @GetMapping("/list")
    public TableDataInfo list(FirmCommodity firmCommodity)
    {
        startPage();
        List<FirmCommodity> list = firmCommodityService.selectFirmCommodityList(firmCommodity);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("purchase:commodity:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FirmCommodity firmCommodity)
    {
        List<FirmCommodity> list = firmCommodityService.selectFirmCommodityList(firmCommodity);
        ExcelUtil<FirmCommodity> util = new ExcelUtil<FirmCommodity>(FirmCommodity.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("purchase:commodity:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(firmCommodityService.selectFirmCommodityById(id));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("purchase:commodity:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FirmCommodity firmCommodity)
    {
        return toAjax(firmCommodityService.insertFirmCommodity(firmCommodity));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("purchase:commodity:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FirmCommodity firmCommodity)
    {
        return toAjax(firmCommodityService.updateFirmCommodity(firmCommodity));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("purchase:commodity:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(firmCommodityService.deleteFirmCommodityByIds(ids));
    }
}
